#ifndef MYTASK_H
#define MYTASK_H

#include <QObject>
#include <QTreeWidgetItem>
#include <QTimer>
#include <QProcess>

class MyTask : public QObject, public QTreeWidgetItem
{
    Q_OBJECT

    enum COLUMNS {
        COUNTER,
        DELAY,
        STATE,
        COMMAND,
    };

public:
    MyTask(int hours  = 0, int minutes = 1, int seconds = 40,
           QString const &command = "", bool loop = false, bool sooi = true, bool ct = false);

    // GETTER
    int hours() const {return _hours;}
    int minutes() const {return _minutes;}
    int seconds() const {return _seconds;}
    QString const &command() const {return _command;}
    bool loop() const {return _loop;}
    bool sooi() const {return _start_only_one_instance;}
    bool ct() const {return _consecutive_task;}
    int counter() const {return _counter;}

    int delay() const {return duration(_hours, _minutes, _seconds);}

    QString state() const {return text(STATE);}

    void set_state(QString const &state) {
        setText(STATE, state);

        if (state == tr("Running"))
            start();
        else if (state == tr("Paused"))
            pause();
        else if (state == tr("Stop"))
            stop();
    }

    // SETTER
    void set_hours(int hours) {
        _hours = hours;
        setText(DELAY, timestamp(_hours, _minutes, _seconds));
        if (text(STATE) == tr("Stop")) {
            set_counter(duration(_hours, _minutes, _seconds));
            emit counter_changed(_counter, this);
        }
    }

    void set_minutes(int minutes) {
        _minutes = minutes;
        setText(DELAY, timestamp(_hours, _minutes, _seconds));
        if (text(STATE) == tr("Stop")) {
            set_counter(duration(_hours, _minutes, _seconds));
            emit counter_changed(_counter, this);
        }
    }

    void set_seconds(int seconds) {
        _seconds = seconds;
        setText(DELAY, timestamp(_hours, _minutes, _seconds));
        if (text(STATE) == tr("Stop")) {
            set_counter(duration(_hours, _minutes, _seconds));
            emit counter_changed(_counter, this);
        }
    }

    void set_command(QString const cmd) {_command = cmd; setText(COMMAND, cmd);}
    void set_loop(bool loop) {_loop = loop;}
    void set_sooi(bool sooi) {_start_only_one_instance = sooi;}
    void set_ct(bool ct) {_consecutive_task = ct;}

    void set_counter(int counter) {_counter = counter;setText(COUNTER, timestamp(_counter));}  // NO SIGNAL

    void start();
    void pause();
    void stop();

signals:
    void counter_changed(int counter, QTreeWidgetItem *self);
    void finished(QTreeWidgetItem *self);

private:
    int duration(int hours, int minutes, int seconds) const {
        return hours*60*60+minutes*60+seconds;
    }

    QString timestamp(int hours, int minutes, int seconds) const {
        return QString::number(hours).rightJustified(2, '0')+ ":" +
               QString::number(minutes).rightJustified(2, '0')+ ":" +
               QString::number(seconds).rightJustified(2, '0');
    }

    QString timestamp(int counter) const {
        int hours = counter/(60*60);
        counter %= (60*60);
        int minutes = counter/60;
        int seconds = counter%60;
        return timestamp(hours, minutes, seconds);
    }

    void execute();
    void process_exit(int exitCode, QProcess::ExitStatus exitStatus);

    int _hours;
    int _minutes;
    int _seconds;
    QString _command;

    bool _loop;
    bool _start_only_one_instance;
    bool _consecutive_task;

    int _counter;

    QTimer *_timer;
    QList<QProcess*> _pproses;
};

#endif // MYTASK_H
